﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Drawing;

using Styx;
using Styx.Common;
using Styx.Helpers;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.WoWInternals.Misc;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWObjects;
using Styx.Pathing;
using Styx.CommonBot.Frames;
using Styx.CommonBot;

namespace RepCollector
{
    class RepCollector : HBPlugin
    {
        public static string name { get { return "Rep Collector "; } }
        public override string Name { get { return name; } }
        public override string Author { get { return "Hazard"; } }
        private readonly static Version _version = new Version(1, 0);
        public override Version Version { get { return _version; } }
        public override string ButtonText { get { return "Settings"; } }
        public override bool WantButton { get { return false; } }
        public static LocalPlayer Me { get { return StyxWoW.Me; } }

        public override void Pulse()
        {
            Thread.Sleep(1 / 30);
            try
            {
                if (!Me.Combat || !Me.IsDead)
                    PickUpEgg();
                    PickUpDarkSoil();
            }
            catch (ThreadAbortException)
            {
            }
        }

        public static void MoveToLocation(WoWPoint loc)
        {
            while (loc.Distance(Me.Location) > 5)
            {
                Flightor.MountHelper.MountUp();
                Navigator.MoveTo(loc);
            }
        }

        static public void PickUpEgg()
        {
            ObjectManager.Update();
            List<WoWGameObject> objList = ObjectManager.GetObjectsOfType<WoWGameObject>()
                .Where(egg => (egg.Distance2D <= 80 && (egg.Entry == 214945))).OrderBy(egg => egg.Distance).ToList();
            foreach (WoWGameObject egg in objList)
            {
                if (!egg.InLineOfSight) return;
                if (Me.Combat) return;
                MoveToLocation(WoWMovement.CalculatePointFrom(egg.Location, -3));
                Flightor.MountHelper.Dismount();
                Thread.Sleep(1000);
                egg.Interact();
                Thread.Sleep(2000);
                Logging.Write("Onyx Egg collected");
                Flightor.MountHelper.MountUp();
            }
        }

        static public void PickUpDarkSoil()
        {
            ObjectManager.Update();
            List<WoWGameObject> objList = ObjectManager.GetObjectsOfType<WoWGameObject>()
                .Where(darksoil => (darksoil.Distance2D <= 80 && (darksoil.Entry == 210565))).OrderBy(darksoil => darksoil.Distance).ToList();
            foreach (WoWGameObject darksoil in objList)
            {
                if (!darksoil.InLineOfSight) return;
                if (Me.Combat) return;
                MoveToLocation(WoWMovement.CalculatePointFrom(darksoil.Location, -3));
                Flightor.MountHelper.Dismount();
                Thread.Sleep(1000);
                darksoil.Interact();
                Thread.Sleep(2000);
                Lua.DoString("RunMacroText(\"/click StaticPopup1Button1\");");
                Logging.Write("Dark Soil collected");
                Flightor.MountHelper.MountUp();
            }
        }
    }
}